/**
 * @file setting.c
 * @brief 音乐播放器设置功能实现
 */

/*********************
 *      INCLUDE
 *********************/
//导入库
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//导入自定义头文件
#include "setting.h"
#include "lv_demo_music.h"
#include "lv_demo_music_main.h"
#include "lv_demo_music_list.h"
#include "message_popup.h"
#include "lrc_parser.h"
#include "utf-8_to_gbk.h"


/**********************
 *  STATIC PROTOTYPES
 **********************/
static void popup_item_event_cb(lv_event_t * e);
static void popup_close_event_cb(lv_event_t * e);
static void popup_bg_event_cb(lv_event_t * e);
static void custom_info_confirm_cb(lv_event_t * e);
static void import_popup_item_event_cb(lv_event_t * e);
static void import_popup_close_event_cb(lv_event_t * e);
static void import_popup_close_event_cb_to_setting(lv_event_t * e);
static int find_option_index(const char* options, const char* target);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_obj_t * settings_popup_bg = NULL;
static lv_obj_t * import_popup_bg = NULL;

// 外部变量声明
extern lv_obj_t * genre_label;
extern lv_obj_t * condition_label;

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/**
 * @brief 设置图标点击事件回调
 * @param e 事件对象
 */
void settings_icon_click_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        create_settings_popup(lv_event_get_current_target(e));
    }
}

/**
 * @brief 创建设置弹窗
 * @param parent 父对象
 */
void create_settings_popup(lv_obj_t * parent)
{
    LV_UNUSED(parent);

    // 如果已有弹窗，先关闭
    if(settings_popup_bg) {
        lv_obj_del(settings_popup_bg);
        settings_popup_bg = NULL;
    }

    // 创建弹窗背景（半透明遮罩）
    settings_popup_bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(settings_popup_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(settings_popup_bg, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(settings_popup_bg, LV_OPA_50, 0);
    lv_obj_set_style_radius(settings_popup_bg, 0, 0);
    lv_obj_clear_flag(settings_popup_bg, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(settings_popup_bg, LV_OBJ_FLAG_CLICKABLE);

    // 创建弹窗容器
    lv_obj_t * popup = lv_obj_create(settings_popup_bg);

    // 设置弹窗样式
#if LV_DEMO_MUSIC_LARGE
    lv_obj_set_size(popup, 320, 280);
#else
    lv_obj_set_size(popup, 280, 220);
#endif
    lv_obj_align(popup, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(popup, lv_color_white(), 0);
    lv_obj_set_style_radius(popup, 12, 0);
    lv_obj_set_style_pad_all(popup, 0, 0);
    lv_obj_set_flex_flow(popup, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(popup, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);

    // 创建标题栏容器
    lv_obj_t * title_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(title_cont);
    lv_obj_set_size(title_cont, lv_pct(100), 50);
    lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_hor(title_cont, 16, 0);
    lv_obj_set_style_pad_top(title_cont, 8, 0);
    lv_obj_set_style_border_width(title_cont, 0, 0);
    lv_obj_set_style_bg_color(title_cont, lv_color_white(), 0);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(title_cont);
    lv_obj_remove_style_all(back_btn);
    lv_obj_set_size(back_btn, 40, 40);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_0, 0);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_20, LV_STATE_PRESSED);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x504d6d), LV_STATE_PRESSED);
    lv_obj_set_style_radius(back_btn, 20, 0);

    lv_obj_t * back_icon = lv_label_create(back_btn);
    lv_label_set_text(back_icon, LV_SYMBOL_LEFT);
    lv_obj_set_style_text_color(back_icon, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_text_font(back_icon, &lv_font_montserrat_22, 0);
    lv_obj_center(back_icon);

    // 为返回按钮添加事件
    lv_obj_add_event_cb(back_btn, popup_close_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_flag(back_btn, LV_OBJ_FLAG_CLICKABLE);

    // 弹窗标题
    lv_obj_t * title = lv_label_create(title_cont);
    lv_label_set_text(title, "设置选项");
    lv_obj_set_style_text_font(title, font_large, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_pad_left(title, 12, 0);

    // 创建选项列表容器
    lv_obj_t * list_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(list_cont);
    lv_obj_set_size(list_cont, lv_pct(100), lv_pct(100));
    lv_obj_set_flex_flow(list_cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(list_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
    lv_obj_set_style_pad_all(list_cont, 0, 0);
    lv_obj_set_style_border_width(list_cont, 0, 0);

    // 分隔线
    lv_obj_t * separator = lv_obj_create(list_cont);
    lv_obj_remove_style_all(separator);
    lv_obj_set_size(separator, lv_pct(100), 1);
    lv_obj_set_style_bg_color(separator, lv_color_hex(0xe0e0e0), 0);
    lv_obj_set_style_bg_opa(separator, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(separator, 0, 0);
    lv_obj_clear_flag(separator, LV_OBJ_FLAG_CLICKABLE);

    // 创建三个选项按钮
    const char* options[] = {
        "自定义当前歌曲信息",
        "导入歌曲",
        "删除当前歌曲"
    };

    for(int i = 0; i < 3; i++) {
        // 创建选项按钮容器
        lv_obj_t * btn_cont = lv_obj_create(list_cont);
        lv_obj_remove_style_all(btn_cont);
        lv_obj_set_size(btn_cont, lv_pct(100), 55);
        lv_obj_set_flex_flow(btn_cont, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(btn_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        lv_obj_set_style_pad_hor(btn_cont, 16, 0);
        lv_obj_set_style_bg_color(btn_cont, lv_color_white(), 0);
        lv_obj_set_style_bg_color(btn_cont, lv_color_hex(0xf0f0f0), LV_STATE_PRESSED);
        lv_obj_set_style_border_width(btn_cont, 0, 0);

        // 添加底部边框线
        if(i < 2) {
            lv_obj_set_style_border_side(btn_cont, LV_BORDER_SIDE_BOTTOM, 0);
            lv_obj_set_style_border_width(btn_cont, 1, 0);
            lv_obj_set_style_border_color(btn_cont, lv_color_hex(0xe0e0e0), 0);
        }

        // 选项文本标签
        lv_obj_t * btn_label = lv_label_create(btn_cont);
        lv_label_set_text(btn_label, options[i]);
        lv_obj_set_style_text_font(btn_label, font_large, 0);
        lv_obj_set_style_text_color(btn_label, lv_color_hex(0x504d6d), 0);
        lv_obj_set_style_text_align(btn_label, LV_TEXT_ALIGN_LEFT, 0);
        lv_obj_set_style_pad_left(btn_label, 3, 0);

        // 右侧箭头图标
        lv_obj_t * arrow_icon = lv_label_create(btn_cont);
        lv_label_set_text(arrow_icon, LV_SYMBOL_RIGHT);
        lv_obj_set_style_text_color(arrow_icon, lv_color_hex(0xcccccc), 0);
        lv_obj_set_style_text_font(arrow_icon, &lv_font_montserrat_22, 0);
        lv_obj_set_style_pad_left(arrow_icon, 8, 0);

        // 让箭头靠右对齐
        lv_obj_set_flex_grow(btn_label, 1);

        // 为整个选项容器添加点击事件
        lv_obj_add_event_cb(btn_cont, popup_item_event_cb, LV_EVENT_CLICKED, NULL);
        lv_obj_add_flag(btn_cont, LV_OBJ_FLAG_CLICKABLE);
    }
}

/**
 * @brief 创建自定义歌曲信息弹窗
 * @param parent 父对象
 */
void create_custom_info_popup(lv_obj_t * parent)
{
    LV_UNUSED(parent);

    // 如果已有弹窗，先关闭
    if(settings_popup_bg) {
        lv_obj_del(settings_popup_bg);
        settings_popup_bg = NULL;
    }

    // 创建弹窗背景（半透明遮罩）
    settings_popup_bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(settings_popup_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(settings_popup_bg, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(settings_popup_bg, LV_OPA_50, 0);
    lv_obj_set_style_radius(settings_popup_bg, 0, 0);
    lv_obj_clear_flag(settings_popup_bg, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(settings_popup_bg, LV_OBJ_FLAG_CLICKABLE);

    // 创建弹窗容器
    lv_obj_t * popup = lv_obj_create(settings_popup_bg);

    // 设置弹窗样式
#if LV_DEMO_MUSIC_LARGE
    lv_obj_set_size(popup, 320, 280);
#else
    lv_obj_set_size(popup, 280, 220);
#endif
    lv_obj_align(popup, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(popup, lv_color_white(), 0);
    lv_obj_set_style_radius(popup, 12, 0);
    lv_obj_set_style_pad_all(popup, 0, 0);
    lv_obj_set_flex_flow(popup, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(popup, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);

    // 标题栏容器
    lv_obj_t * title_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(title_cont);
    lv_obj_set_size(title_cont, lv_pct(100), 50);
    lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_hor(title_cont, 16, 0);
    lv_obj_set_style_pad_top(title_cont, 8, 0);
    lv_obj_set_style_border_width(title_cont, 0, 0);
    lv_obj_set_style_bg_color(title_cont, lv_color_white(), 0);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(title_cont);
    lv_obj_remove_style_all(back_btn);
    lv_obj_set_size(back_btn, 40, 40);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_0, 0);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_20, LV_STATE_PRESSED);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x504d6d), LV_STATE_PRESSED);
    lv_obj_set_style_radius(back_btn, 20, 0);

    lv_obj_t * back_icon = lv_label_create(back_btn);
    lv_label_set_text(back_icon, LV_SYMBOL_LEFT);
    lv_obj_set_style_text_color(back_icon, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_text_font(back_icon, &lv_font_montserrat_22, 0);
    lv_obj_center(back_icon);

    // 弹窗标题
    lv_obj_t * title = lv_label_create(title_cont);
    lv_label_set_text(title, "自定义歌曲信息");
    lv_obj_set_style_text_font(title, font_large, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_pad_left(title, 12, 0);

    // 分隔线
    lv_obj_t * separator = lv_obj_create(popup);
    lv_obj_remove_style_all(separator);
    lv_obj_set_size(separator, lv_pct(100), 1);
    lv_obj_set_style_bg_color(separator, lv_color_hex(0xe0e0e0), 0);
    lv_obj_set_style_bg_opa(separator, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(separator, 0, 0);
    lv_obj_clear_flag(separator, LV_OBJ_FLAG_CLICKABLE);

    // 网格布局容器
    lv_obj_t * grid_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(grid_cont);
    lv_obj_set_size(grid_cont, lv_pct(100), lv_pct(100));

    // 定义3行2列的网格
    static const lv_coord_t grid_col_dsc[] = {
        LV_GRID_FR(1),
        LV_GRID_FR(1),
        LV_GRID_TEMPLATE_LAST
    };
    static const lv_coord_t grid_row_dsc[] = {
        LV_GRID_CONTENT,
        LV_GRID_CONTENT,
        LV_GRID_CONTENT,
        LV_GRID_TEMPLATE_LAST
    };

    lv_obj_set_grid_dsc_array(grid_cont, grid_col_dsc, grid_row_dsc);
    lv_obj_set_style_pad_all(grid_cont, 16, 0);
    lv_obj_set_style_pad_row(grid_cont, 12, 0);
    lv_obj_set_style_pad_column(grid_cont, 12, 0);

    // 获取当前歌曲信息
    //const char* current_genre = genre_list[track_id];
    const char* current_genre = _lv_demo_music_get_genre(track_id);
    const char* current_condition = condition_list[track_id];

    // 表单数据结构
    static form_data_t form_data;

    // 第一行：流派标签和下拉框
    lv_obj_t * genre_label = lv_label_create(grid_cont);
    lv_label_set_text(genre_label, "流派");
    lv_obj_set_style_text_font(genre_label, font_large, 0);
    lv_obj_set_style_text_color(genre_label, lv_color_hex(0x504d6d), 0);
    lv_obj_set_grid_cell(genre_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    form_data.genre_dd = lv_dropdown_create(grid_cont);
    lv_dropdown_set_options(form_data.genre_dd,
        "流行\n摇滚\n电子\n嘻哈\n爵士\n古典\n民谣\n蓝调\n乡村\n金属\n拉丁\n电子音乐\n世界音乐"
    );

    // 设置当前流派
    if (current_genre) {
        int selected_index = find_option_index("流行\n摇滚\n电子\n嘻哈\n爵士\n古典\n民谣\n蓝调\n乡村\n金属\n拉丁\n电子音乐\n世界音乐",
                                             current_genre);
        if (selected_index >= 0) {
            lv_dropdown_set_selected(form_data.genre_dd, selected_index);
        }
    }

    lv_obj_set_style_text_font(form_data.genre_dd, font_large, 0);
    lv_obj_set_size(form_data.genre_dd, LV_SIZE_CONTENT, 35);
    lv_obj_set_style_border_color(form_data.genre_dd, lv_color_hex(0xcccccc), 0);
    lv_obj_set_style_border_width(form_data.genre_dd, 1, 0);
    lv_obj_set_style_radius(form_data.genre_dd, 4, 0);
    lv_obj_set_style_pad_all(form_data.genre_dd, 6, 0);
    lv_obj_set_grid_cell(form_data.genre_dd, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    // 第二行：运动状况标签和下拉框
    lv_obj_t * condition_label = lv_label_create(grid_cont);
    lv_label_set_text(condition_label, "运动状况");
    lv_obj_set_style_text_font(condition_label, font_large, 0);
    lv_obj_set_style_text_color(condition_label, lv_color_hex(0x504d6d), 0);
    lv_obj_set_grid_cell(condition_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);

    form_data.condition_dd = lv_dropdown_create(grid_cont);
    lv_dropdown_set_options(form_data.condition_dd, "正常\n运动");
    lv_obj_set_style_text_font(form_data.condition_dd, font_large, 0);
    lv_obj_set_size(form_data.condition_dd, LV_SIZE_CONTENT, 35);
    lv_obj_set_style_border_color(form_data.condition_dd, lv_color_hex(0xcccccc), 0);
    lv_obj_set_style_border_width(form_data.condition_dd, 1, 0);
    lv_obj_set_style_radius(form_data.condition_dd, 4, 0);
    lv_obj_set_style_pad_all(form_data.condition_dd, 6, 0);
    lv_obj_set_grid_cell(form_data.condition_dd, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);

    // 设置当前运动状况
    if (current_condition) {
        if (strcmp(current_condition, "运动") == 0) {
            lv_dropdown_set_selected(form_data.condition_dd, 1);
        } else {
            lv_dropdown_set_selected(form_data.condition_dd, 0);
        }
    }

    // 第三行：按钮容器
    lv_obj_t * btn_cont = lv_obj_create(grid_cont);
    lv_obj_remove_style_all(btn_cont);
    lv_obj_set_size(btn_cont, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_obj_set_flex_flow(btn_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_cont, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_top(btn_cont, 10, 0);
    lv_obj_set_grid_cell(btn_cont, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_CENTER, 2, 1);

    // 取消按钮
    lv_obj_t * cancel_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(cancel_btn, 100, 36);
    lv_obj_t * cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "取消");
    lv_obj_set_style_text_font(cancel_label, font_small, 0);
    lv_obj_center(cancel_label);
    lv_obj_add_event_cb(cancel_btn, popup_close_event_cb, LV_EVENT_CLICKED, NULL);

    // 确认按钮
    lv_obj_t * confirm_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(confirm_btn, 100, 36);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x6f8af6), 0);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x5a7ae0), LV_STATE_PRESSED);
    lv_obj_t * confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "确认");
    lv_obj_set_style_text_font(confirm_label, font_small, 0);
    lv_obj_set_style_text_color(confirm_label, lv_color_white(), 0);
    lv_obj_center(confirm_label);

    // 为确认按钮添加事件
    lv_obj_add_event_cb(confirm_btn, custom_info_confirm_cb, LV_EVENT_CLICKED, &form_data);
    lv_obj_add_event_cb(back_btn, import_popup_close_event_cb_to_setting, LV_EVENT_CLICKED, NULL);
}

/**
 * @brief 创建导入歌曲弹窗
 * @param parent 父对象
 */
void create_import_popup(lv_obj_t * parent)
{
    LV_UNUSED(parent);

    // 如果已有弹窗，先关闭
    if(import_popup_bg) {
        lv_obj_del(import_popup_bg);
        import_popup_bg = NULL;
    }

    // 创建弹窗背景（半透明遮罩）
    import_popup_bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(import_popup_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(import_popup_bg, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(import_popup_bg, LV_OPA_50, 0);
    lv_obj_set_style_radius(import_popup_bg, 0, 0);
    lv_obj_clear_flag(import_popup_bg, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(import_popup_bg, LV_OBJ_FLAG_CLICKABLE);

    // 创建弹窗容器
    lv_obj_t * popup = lv_obj_create(import_popup_bg);

    // 设置弹窗样式
#if LV_DEMO_MUSIC_LARGE
    lv_obj_set_size(popup, 320, 280);
#else
    lv_obj_set_size(popup, 280, 220);
#endif
    lv_obj_align(popup, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(popup, lv_color_white(), 0);
    lv_obj_set_style_radius(popup, 12, 0);
    lv_obj_set_style_pad_all(popup, 0, 0);
    lv_obj_set_flex_flow(popup, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(popup, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);

    // 创建标题栏容器
    lv_obj_t * title_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(title_cont);
    lv_obj_set_size(title_cont, lv_pct(100), 50);
    lv_obj_set_flex_flow(title_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(title_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_hor(title_cont, 16, 0);
    lv_obj_set_style_pad_top(title_cont, 8, 0);
    lv_obj_set_style_border_width(title_cont, 0, 0);
    lv_obj_set_style_bg_color(title_cont, lv_color_white(), 0);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(title_cont);
    lv_obj_remove_style_all(back_btn);
    lv_obj_set_size(back_btn, 40, 40);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_0, 0);
    lv_obj_set_style_bg_opa(back_btn, LV_OPA_20, LV_STATE_PRESSED);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x504d6d), LV_STATE_PRESSED);
    lv_obj_set_style_radius(back_btn, 20, 0);

    lv_obj_t * back_icon = lv_label_create(back_btn);
    lv_label_set_text(back_icon, LV_SYMBOL_LEFT);
    lv_obj_set_style_text_color(back_icon, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_text_font(back_icon, &lv_font_montserrat_22, 0);
    lv_obj_center(back_icon);

    // 为返回按钮添加事件
    lv_obj_add_event_cb(back_btn, import_popup_close_event_cb_to_setting, LV_EVENT_CLICKED, NULL);
    lv_obj_add_flag(back_btn, LV_OBJ_FLAG_CLICKABLE);

    // 弹窗标题
    lv_obj_t * title = lv_label_create(title_cont);
    lv_label_set_text(title, "导入歌曲");
    lv_obj_set_style_text_font(title, font_large, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x504d6d), 0);
    lv_obj_set_style_pad_left(title, 12, 0);

    // 创建选项列表容器
    lv_obj_t * list_cont = lv_obj_create(popup);
    lv_obj_remove_style_all(list_cont);
    lv_obj_set_size(list_cont, lv_pct(100), lv_pct(100));
    lv_obj_set_flex_flow(list_cont, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(list_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
    lv_obj_set_style_pad_all(list_cont, 0, 0);
    lv_obj_set_style_border_width(list_cont, 0, 0);

    // 添加分隔线
    lv_obj_t * separator = lv_obj_create(list_cont);
    lv_obj_remove_style_all(separator);
    lv_obj_set_size(separator, lv_pct(100), 1);
    lv_obj_set_style_bg_color(separator, lv_color_hex(0xe0e0e0), 0);
    lv_obj_set_style_bg_opa(separator, LV_OPA_COVER, 0);
    lv_obj_set_style_border_width(separator, 0, 0);
    lv_obj_clear_flag(separator, LV_OBJ_FLAG_CLICKABLE);

    // 创建两个导入选项按钮
    const char* import_options[] = {
        "导入MP3与LRC",
        "导入MP3"
    };

    for(int i = 0; i < 2; i++) {
        // 创建选项按钮容器
        lv_obj_t * btn_cont = lv_obj_create(list_cont);
        lv_obj_remove_style_all(btn_cont);
        lv_obj_set_size(btn_cont, lv_pct(100), 55);
        lv_obj_set_flex_flow(btn_cont, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(btn_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        lv_obj_set_style_pad_hor(btn_cont, 16, 0);
        lv_obj_set_style_bg_color(btn_cont, lv_color_white(), 0);
        lv_obj_set_style_bg_color(btn_cont, lv_color_hex(0xf0f0f0), LV_STATE_PRESSED);
        lv_obj_set_style_border_width(btn_cont, 0, 0);

        // 添加底部边框线
        if(i < 1) {
            lv_obj_set_style_border_side(btn_cont, LV_BORDER_SIDE_BOTTOM, 0);
            lv_obj_set_style_border_width(btn_cont, 1, 0);
            lv_obj_set_style_border_color(btn_cont, lv_color_hex(0xe0e0e0), 0);
        }

        // 选项文本标签
        lv_obj_t * btn_label = lv_label_create(btn_cont);
        lv_label_set_text(btn_label, import_options[i]);
        lv_obj_set_style_text_font(btn_label, font_large, 0);
        lv_obj_set_style_text_color(btn_label, lv_color_hex(0x504d6d), 0);
        lv_obj_set_style_text_align(btn_label, LV_TEXT_ALIGN_LEFT, 0);
        lv_obj_set_style_pad_left(btn_label, 3, 0);

        // 右侧箭头图标
        lv_obj_t * arrow_icon = lv_label_create(btn_cont);
        lv_label_set_text(arrow_icon, LV_SYMBOL_RIGHT);
        lv_obj_set_style_text_color(arrow_icon, lv_color_hex(0xcccccc), 0);
        lv_obj_set_style_text_font(arrow_icon, &lv_font_montserrat_22, 0);
        lv_obj_set_style_pad_left(arrow_icon, 8, 0);

        // 让箭头靠右对齐
        lv_obj_set_flex_grow(btn_label, 1);

        // 为整个选项容器添加点击事件
        lv_obj_add_event_cb(btn_cont, import_popup_item_event_cb, LV_EVENT_CLICKED, NULL);
        lv_obj_add_flag(btn_cont, LV_OBJ_FLAG_CLICKABLE);
    }
}

/**
 * @brief 刷新当前歌曲信息
 */
void refresh_current_song_info(void)
{
    printf("[REFRESH] 开始刷新当前歌曲信息, track_id=%d\n", track_id);

    // 安全检查
    if (!filename_list || track_id >= track_count) {
        printf("[ERROR] 无法刷新歌曲信息: 文件名列表无效\n");
        return;
    }

    // 构建文件路径
    char file_path[512];
    const char* filename = filename_list[track_id];
    if (!filename) {
        printf("[ERROR] 无法刷新歌曲信息: 文件名为NULL\n");
        return;
    }

    // 处理文件扩展名
    if (strstr(filename, ".") == NULL) {
        snprintf(file_path, sizeof(file_path), "%s\\%s.lrc", soft_base_path_resource_lrc, filename);
    } else {
        snprintf(file_path, sizeof(file_path), "%s\\%s", soft_base_path_resource_lrc, filename);
    }

    printf("[REFRESH] 重新加载LRC文件: %s\n", file_path);

    // 重新解析LRC文件
    LrcFile* lrc = lrc_parse_file(utf8_to_gbk(file_path));
    if (!lrc) {
        printf("[WARN] 重新解析LRC文件失败: %s\n", file_path);
        return;
    }

    // 更新全局数组
    if (track_id < track_count) {
        // 释放旧内存
        if (genre_list[track_id]) free(genre_list[track_id]);
        if (condition_list[track_id]) free(condition_list[track_id]);

        // 分配新内存
        genre_list[track_id] = lrc->genre ? strdup(lrc->genre) : strdup("");
        condition_list[track_id] = lrc->condition ? strdup(lrc->condition) : strdup("正常");

        printf("[REFRESH] 全局数组已更新:\n");
        printf("[REFRESH]   流派: %s\n", genre_list[track_id] ? genre_list[track_id] : "(空)");
        printf("[REFRESH]   运动状况: %s\n", condition_list[track_id] ? condition_list[track_id] : "(空)");
    }

    // 更新界面显示
    if (genre_label) {
        const char* display_genre = _lv_demo_music_get_genre(track_id);
        lv_label_set_text(genre_label, display_genre ? display_genre : "");
        printf("[REFRESH] 界面流派标签已更新: %s\n", display_genre ? display_genre : "(空)");
    }

    // 更新界面显示
    if (condition_label) {
        const char* display_condition = condition_list[track_id];
        //const char* display_condition = _lv_demo_music_get_condition(track_id);
        lv_label_set_text(condition_label, display_condition ? display_condition : "");
        printf("[REFRESH] 界面运动标签已更新: %s\n", display_condition ? display_condition : "(空)");
    }

    // 重新加载歌词
    lv_demo_music_lyric_set_track(track_id);

    printf("[REFRESH] 歌词已重新加载\n");

    // 释放LRC结构
    lrc_free(lrc);

    printf("[REFRESH] 歌曲信息刷新完成\n");
    show_message_popup("修改歌曲信息成功", 5000);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/**
 * @brief 弹窗选项回调函数
 * @param e 事件对象
 */
static void popup_item_event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);

    // 获取按钮文本
    lv_obj_t * label = lv_obj_get_child(obj, 0);
    const char * txt = lv_label_get_text(label);

    // 根据点击的选项执行相应操作
    if(strcmp(txt, "自定义当前歌曲信息") == 0) {
        // 关闭设置弹窗
        if(settings_popup_bg) {
            lv_obj_del(settings_popup_bg);
            settings_popup_bg = NULL;
        }
        // 创建自定义信息弹窗
        create_custom_info_popup(lv_scr_act());
    }
    else if(strcmp(txt, "导入歌曲") == 0) {
        // 关闭设置弹窗
        if(settings_popup_bg) {
            lv_obj_del(settings_popup_bg);
            settings_popup_bg = NULL;
        }
        // 创建导入歌曲弹窗
        create_import_popup(lv_scr_act());
    }
    else if(strcmp(txt, "删除当前歌曲") == 0) {
        delete_file(utf8_to_gbk(_lv_demo_music_get_filename(track_id)), 0);
        delete_file(utf8_to_gbk(_lv_demo_music_get_filename(track_id)), 1);
        // 关闭设置弹窗
        if(settings_popup_bg) {
            lv_obj_del(settings_popup_bg);
            settings_popup_bg = NULL;
        }
        show_message_popup("删除歌曲成功，需要重新启动才能刷新歌曲", 5000);
    }
}

/**
 * @brief 关闭设置弹窗事件回调
 * @param e 事件对象
 */
static void popup_close_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        if(settings_popup_bg) {
            lv_obj_del(settings_popup_bg);
            settings_popup_bg = NULL;
        }
    }
}

/**
 * @brief 弹窗背景点击事件
 * @param e 事件对象
 */
static void popup_bg_event_cb(lv_event_t * e)
{
    lv_obj_t * bg = lv_event_get_target(e);
    lv_obj_t * popup = lv_event_get_user_data(e);

    // 删除弹窗和背景
    lv_obj_del(bg);
}

/**
 * @brief 自定义信息确认按钮回调
 * @param e 事件对象
 */
static void custom_info_confirm_cb(lv_event_t * e)
{
    form_data_t * form_data = (form_data_t *)lv_event_get_user_data(e);
    if (!form_data) return;

    // 获取流派下拉框的值
    char genre_buf[32];
    lv_dropdown_get_selected_str(form_data->genre_dd, genre_buf, sizeof(genre_buf));
    char* new_genre = genre_buf;

    // 获取运动状况下拉框的值
    char condition_buf[32];
    lv_dropdown_get_selected_str(form_data->condition_dd, condition_buf, sizeof(condition_buf));
    char* new_condition = condition_buf;

    printf("[CUSTOM INFO] 更新歌曲信息: genre=%s, condition=%s\n",
           new_genre, new_condition);

    // 安全检查全局数组
    if (!filename_list || track_id >= track_count) {
        printf("[ERROR] 文件名列表无效或track_id越界: track_id=%d, track_count=%d\n",
               track_id, track_count);
        return;
    }

    // 正确处理文件名和扩展名
    char file_path[512];
    const char* filename = filename_list[track_id];

    if (!filename) {
        printf("[ERROR] 文件名为NULL，track_id=%d\n", track_id);
        return;
    }

    printf("[DEBUG] 原始文件名: %s\n", filename);

    // 检查是否需要添加.lrc扩展名
    if (strlen(filename) > 0) {
        if (strstr(filename, ".") == NULL) {
            snprintf(file_path, sizeof(file_path), "%s\\%s.lrc", soft_base_path_resource_lrc, filename);
        } else {
            snprintf(file_path, sizeof(file_path), "%s\\%s", soft_base_path_resource_lrc, filename);
        }
    } else {
        printf("[ERROR] 文件名为空字符串\n");
        return;
    }

    printf("[DEBUG] 完整文件路径: %s\n", file_path);

    // 文件存在性检查
    FILE* test_file = fopen(utf8_to_gbk(file_path), "r");
    if (!test_file) {
        printf("[WARN] LRC文件不存在");
        return;
    } else {
        printf("[INFO] LRC文件已存在: %s\n", file_path);
         // LRC文件处理
        LrcFile* lrc = lrc_parse_file(utf8_to_gbk(file_path));
        if (!lrc) {
            printf("[WARN] 无法解析LRC文件 %s，创建新结构\n", file_path);

        // 创建新的LRC结构
            lrc = (LrcFile*)malloc(sizeof(LrcFile));
            if (!lrc) {
                printf("[ERROR] 无法创建LRC结构\n");
                return;
            }

            // 初始化新结构的基本字段
            memset(lrc, 0, sizeof(LrcFile));
            lrc->title = strdup("");
            lrc->artist = strdup("");
            lrc->album = strdup("");
            lrc->by = strdup("");
            lrc->genre = strdup("");
            lrc->condition = strdup("");
            lrc->line_count = 0;
            lrc->lines = NULL;
        }

        // 内存管理宏
        #define SAFE_FREE(ptr) do { if (ptr) { free(ptr); ptr = NULL; } } while(0)
        #define SAFE_STRDUP(str) ((str) ? strdup(str) : strdup(""))

        // 更新LRC文件信息
        SAFE_FREE(lrc->genre);
        lrc->genre = SAFE_STRDUP(new_genre);
        SAFE_FREE(lrc->condition);
        lrc->condition = SAFE_STRDUP(new_condition);

        // 保存回文件
        if (lrc_save_to_file(lrc, utf8_to_gbk(file_path)) != 0) {
            printf("[ERROR] 保存LRC文件失败 %s\n", file_path);
            lrc_free(lrc);
            return;
        }

        printf("[SUCCESS] LRC文件保存成功: %s\n", file_path);
        printf("[SUCCESS] 更新内容 -  流派: %s, 状况: %s\n",
        lrc->genre ? lrc->genre : "(空)",
        lrc->condition ? lrc->condition : "(空)");
        // 释放LRC结构
        lrc_free(lrc);
        // 安全更新全局数组
        if (track_id < track_count) {
            #define UPDATE_GLOBAL_ARRAY(arr, new_val) do { \
                    if (arr[track_id]) free(arr[track_id]); \
            arr[track_id] = SAFE_STRDUP(new_val); \
            } while(0)

            UPDATE_GLOBAL_ARRAY(genre_list, new_genre);
            UPDATE_GLOBAL_ARRAY(condition_list, new_condition);

            printf("[SUCCESS] 全局数组更新完成\n");
        } else {
            printf("[ERROR] track_id超出范围，无法更新全局数组\n");
        }
        // 关闭弹窗
        if(settings_popup_bg) {
            lv_obj_del(settings_popup_bg);
            settings_popup_bg = NULL;
        }
        // 重新加载并刷新当前歌曲信息
        refresh_current_song_info();
    }
    fclose(test_file);
}


/**
 * @brief 导入弹窗选项回调函数
 * @param e 事件对象
 */
static void import_popup_item_event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);

    // 获取按钮文本
    lv_obj_t * label = lv_obj_get_child(obj, 0);
    const char * txt = lv_label_get_text(label);

    // 根据点击的选项执行相应操作
    if(strcmp(txt, "导入MP3与LRC") == 0) {
        mp3_import_main(1);
        if(import_popup_bg) {
            lv_obj_del(import_popup_bg);
            import_popup_bg = NULL;
        }
    }else if(strcmp(txt, "导入MP3") == 0) {
        mp3_import_main(0);
        if(import_popup_bg) {
            lv_obj_del(import_popup_bg);
            import_popup_bg = NULL;
        }
    }
}

/**
 * @brief 导入弹窗关闭事件回调
 * @param e 事件对象
 */
static void import_popup_close_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        if(import_popup_bg) {
            lv_obj_del(import_popup_bg);
            import_popup_bg = NULL;
        }
    }
}

/**
 * @brief 导入弹窗关闭事件回调（返回一级设置）
 * @param e 事件对象
 */
static void import_popup_close_event_cb_to_setting(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        if(import_popup_bg) {
            lv_obj_del(import_popup_bg);
            import_popup_bg = NULL;
        }
        create_settings_popup(lv_scr_act());
    }
}

/**
 * @brief 在选项字符串中查找文本的索引
 * @param options 选项字符串
 * @param target 目标文本
 * @return 索引号，未找到返回-1
 */
static int find_option_index(const char* options, const char* target)
{
    if (!options || !target) return -1;

    const char* ptr = options;
    int index = 0;
    char buffer[64];

    while (*ptr) {
        // 找到下一个换行符或字符串结束
        const char* end = strchr(ptr, '\n');
        if (!end) end = ptr + strlen(ptr);

        // 提取当前选项
        int len = end - ptr;
        if (len >= (int)sizeof(buffer)) len = sizeof(buffer) - 1;
        strncpy(buffer, ptr, len);
        buffer[len] = '\0';

        // 比较是否匹配
        if (strcmp(buffer, target) == 0) {
            return index;
        }

        // 移动到下一个选项
        index++;
        ptr = (*end == '\n') ? end + 1 : end;
    }

    return -1;  // 未找到
}
